<? session_start();
						
include_once ('core/conn.php');
						
$db = get_conn();
						
check_session();
						
if($_GET['id'] != ''){
							
	$sql = " select * from invoices where id_invoice=".$_GET['id']; 
							
	$result = mysql_query($sql);	
							
	if($result){
								
		$row = mysql_fetch_array($result);
		$id_invoice = $row['id_invoice'];
		$id_location = $row['id_location'];
		$id_it = $row['id_it'];
		$amount = $row['amount'];
		$due_date = $row['due_date'];
		$exp_date = $row['exp_date'];
		$description = $row['description'];
		$istatus = $row['istatus'];
							
	}else{
								
		error_handling($sql,mysql_error());
							
	}
						
}
						
						
					
?>
					
					
<html>
					
<head>
<script src="tools/jq_dp/js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="tools/jq_dp/js/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>
<link href="http://jqueryui.com/demos/demos.css" rel="stylesheet" type="text/css" />
<script>
<!--


	function CheckAll(){
	
		for(i=0;i<document.getElementsByTagName("input").length;i++){
			if(document.getElementsByTagName("input")[i].type == "checkbox"){
				document.getElementsByTagName("input")[i].checked =true;				
			}
		}
	}
	
	function UnCheckAll(chk){
		
		
		for(i=0;i<document.getElementsByTagName("input").length;i++){
			if(document.getElementsByTagName("input")[i].type == "checkbox"){
				document.getElementsByTagName("input")[i].checked =false;				
			}
		}
	}	

					
						
	function checkdata(obj){
							
		if(obj.id_it.value ==''){
			alert('Debe seleccionar el tipo de cobro');
			obj.id_it.focus();
			return false;
		}
		
		if(obj.due_date.value ==''){
			alert('Debe seleccionar la fecha de cobro');
			obj.due_date.focus();
			return false;
		}
		
		if(obj.exp_date.value ==''){
			alert('Debe seleccionar la fecha de vencimiento');
			obj.exp_date.focus();
			return false;
		}		
		
		var none_selected = true;
		
		for(i=0;i<document.getElementsByTagName("input").length;i++){
			if(document.getElementsByTagName("input")[i].type == "checkbox"){
				if(document.getElementsByTagName("input")[i].checked ==true){
					none_selected = false;
				}				
			}
		}		
		if(none_selected){
			alert('Debe seleccionar alguna fileal');
			//obj.id_it.focus();
			return false;
		}
	return true;
	}
	

	function show_results(){
		var itype= document.getElementById('id_it').value;
		var due_date= document.getElementById('due_date').value;
		var exp_date= document.getElementById('exp_date').value;	 	 
		var id_location= document.getElementById('id_location').value;	 	 
		var url = "ajax_comps/history.php?l="+id_location+"&t="+itype+"&d="+due_date+"&e="+exp_date;
		$("#list_layer").load(url);	
	}


	$(function(){
		$( "#due_date" ).datepicker();
		$( "#exp_date" ).datepicker();
	});





-->
</script>					
</head>									
					
<body>														
				
				
<form name='invoices' method='post' action='/core/invoices.php' onsubmit='return checkdata(this)'>
  <table width='100%' border='0'>
    <tr>
      <td width="100" align='left' nowrap>Historial de Cobros</td>
    </tr>
    <tr>
      <td align='left' nowrap>
<table width="100%" border="0">

  <tr>
    <td><? 
		if($_GET['t']=='ip'){
			$filter  = "istatus='P' and ";
		}else{
			$filter  = "istatus='R' and ";
		}
		
		$id_location = $_GET['id_location'];
		
		$sql = "select i.id_invoice, i.amount, i.due_date, i.exp_date, i.description, l.number, l.id_location, it.name, i.description
				from 
				`invoices` i , 
				`locations` l ,
				`invoices_type` it
				where 
				l.id_location =".$id_location." and 
				$filter
				i.id_location = l.id_location and
				i.id_it = it.id_it
				order by i.id_invoice";
			
		$result = mysql_query($sql);
			
		if($result){
				
			$data ="<table width='100%' border='0' align='left' >";
				
			if(mysql_num_rows($result) > 0){
					$data .="<tr>

						<td align='left' >
							ID
						</td>
						<td align='left' >
							Filial
						</td>							
						<td align='left' >
							Tipo
						</td>
						
						<td align='left' >
							Fecha Cobro
						</td>						
						
						<td align='left' >
							Fecha Pago
						</td>
						
						<td align='left' >
							Recibo
						</td>	
						<td align='left' >
							Comentarios
						</td>																								
						</tr>";					
				while($row = mysql_fetch_array($result)){
					//$owner_name = get_owner_name($row['id_location']);
					$data .="<tr>

						<td align='left' >
							".$row['id_invoice']."
						</td>						
						<td align='left' >
							".$row['number']." 
						</td>
						<td align='left' >
							".$row['name']."
						</td>													
						<td align='left' >
							".format_date($row['due_date'])."
						</td>
						<td align='left' >
							".format_date($row['exp_date'])."
						</td>
						<td align='center' >
							".format($row['amount'],2)."
						</td>	
						<td align='left' >
							".$row['description']."
						</td>																								
						</tr>";
						
						$total = $total + $row['amount'];
					
				}//while
				
					$data .="<tr>

						<td align='left' colspan='7'>
							<hr size='1'>
						</td>						
						</tr>";
										
					$data .="<tr>

						<td align='right' colspan='5'>
							Total
						</td>						
						<td align='center' >
							".format($total,2)."
						</td>	
						<td align='left' >
							&nbsp;
						</td>																								
						</tr>";
				
				
			}//if
				
			else{
					
				$data .= "<tr><td align='center'>No hay cobros pendientes</td></tr>";			
				
			}
				
			$data .="</table>";
			
		}else{
				
			error_handling($sql,mysql_error());
			
		}
			
		echo $data;
			
		?>
      &nbsp;</td>
  </tr>
</table>
      
      </td>
    </tr>
    <tr>
      <td align='left' nowrap>&nbsp;</td>
    </tr>
  </table>
</form>
</body></html>